// 封装的工具类，用于使用easyexcel导出excel

//导出excel方法  url:接口地址   methods:请求方式（默认是走post）   data:需要携带的数据   fileName:导出的文件名
export default {
  download(uri, methods, data, fileName) {
    console.log(uri, methods, data, fileName);
    let requestInstance = new Request("http://47.100.172.225:9999" + uri, {
      method: methods,
      responseType: "blob",
      headers: {
        Authorization: localStorage.getItem("token"),
        "Content-Type": "application/json;charset=utf-8"
      },
      body: JSON.stringify(data)
    });
    fetch(requestInstance)
      .then(res => {
        return res.blob();
      })
      .then(res => {
        let blob = new Blob([res]);
        let downloadElement = document.createElement("a");
        let href = window.URL.createObjectURL(blob); //创建下载的链接
        downloadElement.href = href;
        downloadElement.download = fileName + ".xlsx"; //下载后文件名
        document.body.appendChild(downloadElement);
        downloadElement.click(); //点击下载
        document.body.removeChild(downloadElement); //下载完成移除元素
        window.URL.revokeObjectURL(href); //释放掉blob对象
      });
  }
};
